<?xml version='1.0' standalone="no"?>
<%@page import="backend.db.ORM"%>
<%@page import="backend.Session"%>
<%@page import="backend.Course"%>
<%@page import="java.util.HashMap" %>
<%@ page language="java" contentType="text/xml; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%
	String username = request.getUserPrincipal().getName();
	Iterable<Course> courses = ORM.getMyCourses(username);
	Iterable<Session> sessions = ORM.getAllUserCoursesSessions(courses);
	int size = 0;
	out.write("<!DOCTYPE timetable SYSTEM \"timetable.dtd\">\n");
	out.write("<timetable>\n");
	out.write("<table>\n");
	String[] colors = { "#FF0000", "#00FF00", "#FFFF00", "#00FFFF", "#FF00FF", "#C0C0C0", "#CC6600", "#CC00CC"};
	int sOfColorsArr = colors.length;
	HashMap<String, String> existingCourses = new HashMap<String, String>();
	for(int i=0; i<10; i++)
	{
		out.write("<line>\n");
		out.write("<time>");
		int time = i+8;
		String hours1 = "";
		String hours2 = String.valueOf(time+1);
		if(time<10)
		{
			hours1 = "0"+String.valueOf(time);
			if(time == 8)
				hours2 = "09";
				
		}
		else
			hours1 = String.valueOf(time);
		out.write(hours1+":00-" + hours2 + ":00");
		out.write("</time>\n");
		for(int j=0; j<7; j++)
		{
			boolean isCellFull = false;
			for (Session s : sessions) {
				Course c = ORM.getCourseById(s.courseId);
				if(s.day.ordinal() == j && s.startHour ==(i+8))
				{
					if(!existingCourses.containsKey(c.courseName))
					{
						int newColorIndex = existingCourses.size() % sOfColorsArr;
						existingCourses.put(c.courseName, colors[newColorIndex]);
					}
					isCellFull = true;
					String type="tutorial";
					if(s.isLecture == true)
						type="lecture";
					out.write("<cell>\n");
					out.write("<courseName>"+c.courseName+"</courseName>\n");
					out.write("<courseid>"+s.courseId+"</courseid>\n");
					out.write("<room>"+s.classLocation+"</room>\n");
					out.write("<type>"+type+"</type>\n");
					out.write("<duration>"+s.duration+"</duration>\n");
					out.write("<color>"+existingCourses.get(c.courseName)+"</color>\n");
					out.write("</cell>\n");
				}
				else if(s.day.ordinal() == j&& s.startHour+s.duration >=(i+8+1) && s.startHour <=(i+8))
						isCellFull = true;
			}
			if(!isCellFull)
				out.write("<cell><duration>0</duration></cell>\n");
		}
		out.write("</line>\n");
	}
	
	out.write("</table>\n");
	out.write("</timetable>\n");
%>